package suite.test
{
	import com.aflac.model.ContentUpdateModel;
	import com.aflac.model.StorageModel;
	
	import flash.events.EventDispatcher;
	
	import flexunit.framework.Assert;
	
	import org.as3commons.logging.ILogger;
	import org.as3commons.logging.LoggerFactory;
	import org.flexunit.asserts.assertEquals;
	import org.swizframework.storage.EncryptedLocalStorageBean;

	public class StorageModelShould
	{
		public static const LOGGER : ILogger = LoggerFactory.getClassLogger(StorageModelShould);
	
		private var storageModel:StorageModel;
		
		
		//--------------------------------------------------------------------------
		//
		//  Before and After
		//
		//--------------------------------------------------------------------------
		
		[Before]
		public function setUp():void 
		{
			
			LOGGER.info("setUp");
			storageModel = new StorageModel();
			storageModel.els = new EncryptedLocalStorageBean();
			storageModel.els.reset();
			storageModel.contentUpdateModel = new ContentUpdateModel();
			storageModel.dispatcher = new EventDispatcher();

		}
		
		[After]
		public function tearDown():void 
		{
			LOGGER.info("tearDown");
		
			storageModel = null;
		}       
		
		//--------------------------------------------------------------------------
		//
		//  Tests
		//
		//--------------------------------------------------------------------------
		
		[Test]
		public function respondFalseFor_isValidGuidForOfflineAccess_WhenGuidIsNull():void
		{
			LOGGER.info("respondFalseFor_isValidGuidForOfflineAccess_WhenGuidIsNull");
			Assert.assertNull(storageModel.guid);
			Assert.assertFalse( storageModel.isValidGuidForOfflineAccess() );
		}
		
		[Test]
		public function respondFalseFor_isValidGuidForOfflineAccess_WhenLastValidGuidIsThreeOrMoreDaysOld():void
		{
			LOGGER.info("respondFalseFor_isValidGuidForOfflineAccess_WhenLastValidGuidIsThreeOrMoreDaysOld");
			storageModel.guid = "somerandomeguidstring";
			storageModel.guidDate = new Date(1999);
			Assert.assertFalse( storageModel.isValidGuidForOfflineAccess() );
		}
		
		
		[Test]
		public function respondTrueFor_isValidGuidForOfflineAccess_WhenLastValidGuidIsLessThanThreeDaysOld():void
		{
			LOGGER.info("respondTrueFor_isValidGuidForOfflineAccess_WhenLastValidGuidIsLessThanThreeDaysOld");
			storageModel.guid = "somerandomeguidstring";
			Assert.assertTrue( storageModel.isValidGuidForOfflineAccess() );
		}
		
		[Test]
		public function respondFalseFor_shouldAllowLoginAttempt_IfNotEnoughTimeHasPassedAfterReachingMaxAttempt():void
		{
			LOGGER.info("respondFalseFor_shouldAllowLoginAttempt_IfNotEnoughTimeHasPassedAfterReachingMaxAttempt");
			storageModel.loginCounter = StorageModel.MAXIMUM_LOG_IN_ATTEMPTS;
			Assert.assertFalse( storageModel.shouldAllowLoginAttempt() );
		}
		
		[Test]
		public function respondTrueFor_shouldAllowLoginAttempt_IfEnoughTimeHasPassedAfterReachingMaxAttempt():void
		{
			LOGGER.info("respondTrueFor_shouldAllowLoginAttempt_IfEnoughTimeHasPassedAfterReachingMaxAttempt");
			storageModel.loginCounter = StorageModel.MAXIMUM_LOG_IN_ATTEMPTS;
			storageModel.dateReachedMaxLoginAttempt = new Date(1999);
			Assert.assertTrue( storageModel.shouldAllowLoginAttempt() );
		}
		
		[Test]
		public function beAbleTo_setStoredDataToUpdateData_IfNoDataStored():void
		{
			var fakeData:Object = new Object();
			storageModel.contentUpdateModel.updateData = fakeData;
			storageModel.mergeStoredDataWithUpdateData();
			Assert.assertObjectEquals(fakeData, storageModel.contentData);
		}
		
		[Test]
		public function beAbleTo_mergeStoredDataWithUpdateData():void
		{
			var fakeContentData:Object = new Object();
			var presentModule:Object = new Object();
			presentModule.contentTemplates = [{id:'a'}, {id:'b'}, {id:'c'}];
			presentModule.industries = [{id:'a'},  {id:'b'}, {id:'c'}];
			presentModule.supportContent = [{id:'a'},  {id:'b'}, {id:'c'}];
			presentModule.videoGroups = [{id:'a'},  {id:'b'}, {id:'c'}];
			presentModule.states = [{id:'a'},  {id:'b'}, {id:'c'}];
			presentModule.assets = [{id:'a', value:"1"},  {id:'b', value:"2"}, {id:'c', value:"3"}];
			presentModule.name = "PRESENT";
			fakeContentData.modules = [presentModule, {name:"RECRUIT"}];
			storageModel.contentData = fakeContentData;
			
			var fakeUpdateData:Object = new Object();
			var presentUpdateModule:Object = new Object();
			presentUpdateModule.contentTemplates = [{id:'d'}, {id:'e'}, {id:'f'}];
			presentUpdateModule.industries = [{id:'d'}, {id:'e'}, {id:'f'}];
			presentUpdateModule.supportContent = [{id:'d'}, {id:'e'}, {id:'f'}];
			presentUpdateModule.videoGroups = [{id:'d'}, {id:'e'}, {id:'f'}];
			presentUpdateModule.states = [{id:'d'}, {id:'e'}, {id:'f'}];
			presentUpdateModule.assets = [{id:'a', value:"4"}, {id:'c', value:"6"}, {id:'d', value:"7"}];
			presentUpdateModule.name = "PRESENT";
			fakeUpdateData.modules = [presentUpdateModule, {name:"RECRUIT"}];
			storageModel.contentUpdateModel.updateData = fakeUpdateData;
			
			storageModel.mergeStoredDataWithUpdateData();
			
			var mergedPresentModule:Object = storageModel.getPresentModuleOfContentData(storageModel.contentData);
			
			Assert.assertObjectEquals(presentUpdateModule.contentTemplates, mergedPresentModule.contentTemplates);
			Assert.assertObjectEquals(presentUpdateModule.industries, mergedPresentModule.industries);
			Assert.assertObjectEquals(presentUpdateModule.supportContent, mergedPresentModule.supportContent);
			Assert.assertObjectEquals(presentUpdateModule.videoGroups, mergedPresentModule.videoGroups);
			Assert.assertObjectEquals(presentUpdateModule.states, mergedPresentModule.states);
			Assert.assertObjectEquals([{id:'a', value:"4"}, {id:'b', value:"2"},{id:'c', value:"6"}, {id:'d', value:"7"}], mergedPresentModule.assets);
		}
		
		[Test]
		public function beAbleTo_updateContentVersion_IfUpdatedValueExists():void
		{
			var version:String = "1.1"; 
			storageModel.contentUpdateModel.versionOnServer = version;
			storageModel.updateContentVersion();
			Assert.assertEquals(version, storageModel.contentDataVersion);
		}
		
		[Ignore("Ignore until service response is updated to include a viewed property")]
		[Test]
		public function beAbleTo_getNumberOfSharedPresentationsToBeViewed():void
		{
		
		}
		
		[Ignore("Ignore until service response is updated to include an id value")]
		[Test]
		public function beAbleTo_getPresentationById():void
		{
		
		}
		[Ignore("Ignore until service response is updated to include an id value")]
		[Test]
		public function beAbleTo_removePresentationById():void
		{
		
		}
		
	}
}